package org.Image.management.SpringDataService.Service.Sys_User.Service;


import cn.hutool.crypto.digest.DigestUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.Image.management.SpringDataService.CopyUtils.CopyUserUtils;
import org.Image.management.SpringDataService.Pojo.Exception.BusinessException;
import org.Image.management.SpringDataService.Pojo.Exception.StatusCode;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.Vo.UpdateVo;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.dto.LoginRequest;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.dto.RegisterRequest;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.mapper.User;
import org.Image.management.SpringDataService.Service.Sys_User.Service.impl.UserServiceInterface;
import org.Image.management.SpringDataService.Service.Sys_User.dataService.impl.UserDataServiceInterface;
import org.Image.management.utils.GetLocalDateTimeUtils;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserService implements UserServiceInterface {
    // 数据库操作
    private final UserDataServiceInterface userDataServiceinterface;

    // 复制
    private final CopyUserUtils copyUserUtils;

    @Override
    public User registerUser(RegisterRequest registerRequest) {
        //处理用户密码,防止防暴力破解并增强安全性
        registerRequest.setPassword(DigestUtil.md5Hex(registerRequest.getPassword()));
        //RegisterRequest -转-> User
        User addUser = copyUserUtils.RegisterRequestToUser(registerRequest);

        // 数据库 添加用户数据
        return userDataServiceinterface.addUser(addUser);
    }

    @Override
    public User loginUser(LoginRequest user) {
        String PasswordMD5 = DigestUtil.md5Hex(user.getPassword());

        return userDataServiceinterface.login(user.getName(), PasswordMD5,false);
    }

    @Override
    public UpdateVo modifyPassword(Long ID, String oldPassword, String newPassword) {
        // 判断旧密码和新密码是否相同
        if (oldPassword.equals(newPassword)) {
            throw new BusinessException(StatusCode.BAD_REQUEST, "新密码不能与旧密码相同");
        }
        //判断旧密码是否正确
        String PasswordMD5 = DigestUtil.md5Hex(oldPassword);
        if (!userDataServiceinterface.isUserPasswordFind(ID, PasswordMD5)) {
            throw new BusinessException(StatusCode.BAD_REQUEST, "旧密码错误");
        }
        // 封装新密码
        User build = User.builder().id(ID).password(DigestUtil.md5Hex(newPassword)).build();

        //修改密码
        User user = userDataServiceinterface.updateUser(build);
        log.info("修改密码 测试 {}", user);

        return UpdateVo.builder().id(ID).updatedDate(GetLocalDateTimeUtils.GetLocalDateTimeToStrings()).build();
    }

    @Override
    public UpdateVo modifyUserName(Long ID, String newUserName) {
        // 判断新用户名是否已存在
        if (userDataServiceinterface.isUsernameFind(newUserName)) {
            throw new BusinessException(StatusCode.BAD_REQUEST, "用户名已存在,请重新输入!");
        }

        // 封装新用户名
        User build = User.builder().id(ID).userName(newUserName).build();
        // 修改用户名
        userDataServiceinterface.updateUser(build);
        return UpdateVo.builder().id(ID).updatedDate(GetLocalDateTimeUtils.GetLocalDateTimeToStrings()).build();
    }

    @Override
    public boolean deleteUser(Long id, boolean ConfirmDelete) {
        userDataServiceinterface.updateUser(User.builder().id(id).userDelete(ConfirmDelete).build());
        return true;
    }
    public boolean deleteUser( LoginRequest user) {
        String PasswordMD5 = DigestUtil.md5Hex(user.getPassword());

        User login = userDataServiceinterface.login(user.getName(), PasswordMD5, true);
        log.info("删除用户{}",login);

        deleteUser(login.getId(),false);

        return true;
    }
}
